<template>
	<view>
		<block>
			<uni-nav-bar @clickLeft="goBack" status-bar="true" shadow="true" left-icon="back" left-text="返回" title="消息"></uni-nav-bar>
		</block>
		<uni-list>
			<uni-swipe-action>
			<scroll-view upper-threshold="50" @scrolltoupper="refreshOne" :style="{height:scrowviewheight+'px'}" scroll-y="true"
			 @scrolltolower="loadmore()">
			<block v-for="(item,index2) in myNews" :key="index2">
				<uni-swipe-action-item :options="options1" @click="bindClick(item,index2)">
				
				<view class="uni-media-list uni-list-cell">
					<view class="uni-media-list-body" @tap="goDetail(item)">
						<view class="uni-media-list-text-bottom">
							<view style="display: inherit;" >
								<image class="uni-media-list-logo" style=" border-radius:200px; " :src="item.commentator.avatar_url"></image>
								<text style="color: #007AFF; font-weight: bold;">{{ item.commentator.name}}</text>
								<text style="font-size: smaller; padding-left: 5px;"  >评论了</text>
							</view>
							<text>{{item.createdAt | FormatDate('yyyy-MM-dd HH:mm:ss') }}</text>
						</view>
						<view class="uni-media-list-text-top">{{ item.comment.content }}</view>
						<view class="uni-media-list-text-top2" style="background-color: #EEEEEE;">在{{item.topic.location}},{{item.topic.clinic}},看的{{item.topic.diseseName}} </view>
					</view>
				</view>
				</uni-swipe-action-item>
				
			</block>
			<!-- 上拉加载 -->
			<view class="load-more">{{loadmoreText}}</view>
			</scroll-view>
			</uni-swipe-action>
		</uni-list>
		<image @tap="refresh" class="refresh-button icon iconfont icon-shuaxin" />
	</view>
</template>

<script>
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
	import config from "@/common/config.js"
	// import utils from "@/common/util.js"
	import uniSwipeAction from '@/components/uni-swipe-action/uni-swipe-action.vue'
	import uniSwipeActionItem from '@/components/uni-swipe-action-item/uni-swipe-action-item.vue'
	export default {
		components: {
			uniNavBar,
			uniSwipeAction,
			uniSwipeActionItem
			
		},
		data() {
			return {
				myNews: [],
				options1: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}],
				scrowviewheight: 300,
				loadmoreText: '上拉加载更多',
				isLoadingMore: false,
				currentPage: 1,
			}
		},
		onLoad() {
			uni.getSystemInfo({
				success: (res) => {
					//减去顶部tab，底部tabbar，顶部搜索,顶部搜索margin-top
					let windowHight = res.windowHeight - uni.upx2px(100);
					this.scrowviewheight = windowHight;
				}
			});
			this.initData(1);
			
		},
		onPullDownRefresh() {
			this.currentPage = 1;
			this.initData(1);
		},
		methods: {
			loadmore() {
				console.log("dfdfdfd=====")
				if (!this.isLoadingMore && this.loadmoreText != "没有更多数据") {
					this.isLoadingMore = true;
					this.loadmoreText = "数据加载中..";
					this.initData(this.currentPage);
				}
			
			},
			refreshOne() {
				console.log("refreshOne====")
			},
			refresh() {
				this.currentPage = 1;
				this.initData(1);
			// 	uni.startPullDownRefresh({
			// 	})
			},
			bindClick(item,index2) {
				this.deleteNews(item,index2);
			},
			goDetail(item) {
				var rootTopicId = item.rootTopicId;
				var currentTopicId = item.topic._id;
				console.log('ueeeee:'+item)
				console.log('uuuuuuu:'+currentTopicId)
				var commentId = item.comment._id;
				var rootCommentId = item.comment.rootCommentId;
				console.log(item);
				console.log(currentTopicId);
				console.log(rootCommentId);
				if(rootCommentId){
					var url = '../commentdetils/commentdetils?topicId=' + currentTopicId + '&commentId=' + rootCommentId+'&rootTopicId='+rootTopicId+'&commentIdFromNewspage='+commentId;
				}else{
					var url = '../recorderdetail/recorderdetail?id=' + rootTopicId+'&currentTopicId='+currentTopicId+'&commentId='+commentId;
				}
				uni.redirectTo({
					url
				})
				
			},
			initData(page) {
				
				var url = config.baseUrl + '/news?page='+page; //没必要做加载更多
				console.log(url);
				var token = uni.getStorageSync(config.token);
				uni.request({
					method: 'GET',
					url: url,
					header: {
						'content-type': 'application/json', //自定义请求头信息
						'Authorization': 'Bearer ' + token //通过登录接口，生成新的token
					},
					success: (res) => {
						var statusCode = res.statusCode;
						if (200 == statusCode) {
							// this.myNews = res.data;
							if (page === 1) {
								this.myNews = res.data;
							} else {
								this.myNews = this.myNews.concat(res.data);
							}
							var d = this.myNews[0];
							console.log('fffdfdfqqqqqq');
							console.log(d);
							// var dd =  utils.formatUTC(d.createdAt,'yyyy-MM-dd HH:mm:ss');
							// console.log(dd);
							if (res.data.length > 0) {
								this.loadmoreText = "上拉加载更多";
								this.currentPage = this.currentPage + 1;
							} else {
								this.loadmoreText = "没有更多数据";
								console.log(this.currentPage + ':没有更多数据');
							}
						} else if (401 == statusCode) {
							this.loadmoreText = "上拉加载失败";
							uni.navigateTo({
								url: '../authorityRequst/authorityRequst'
							})
						} else {
							this.loadmoreText = "上拉加载失败";
							console.log("wod err");
						}
					},
					fail: (res) => {
						console.log("wod fafas");
					},
					complete: () => {
						this.isLoadingMore = false;
						uni.stopPullDownRefresh();
					}
				});
			},
			deleteNews(item,index){
				uni.showLoading({
					title: 'loading'
				});
				var id = item._id;
					var url = config.baseUrl + '/news/'+id; //没必要做加载更多
					console.log(url);
					var token = uni.getStorageSync(config.token);
					uni.request({
						method: 'DELETE',
						url: url,
						header: {
							'content-type': 'application/json', //自定义请求头信息
							'Authorization': 'Bearer ' + token //通过登录接口，生成新的token
						},
						success: (res) => {
							var statusCode = res.statusCode;
							console.log(statusCode);
							if (204 == statusCode) {
								this.myNews.splice(index,1);
								uni.showToast({
									title: `删除成功`,
									icon: 'none'
								})
							} else if (401 == statusCode) {
								this.checkSession();
							} else {
								console.log("wod err");
							}
						},
						fail: (res) => {
							console.log("wod fafas");
						},
						complete: () => {
						uni.hideLoading();
						}
					});
				
			},
			goBack() {
				uni.navigateBack({

				})
			},
			checkSession(){
				uni.checkSession({
					success: (e) => {
						var weixinsession = uni.getStorageSync(config.sessionkey);
						if (weixinsession.length > 0) {
							uni.navigateTo({
								url: '../authorityRequst/authorityRequst'
							})
						} else {
							uni.navigateTo({
								url: '../authorityRequst/authorityRequst'
							})
						}
				
					},
					fail: () => {
						console.log("yijinggoqdi")
						uni.navigateTo({
							url: '../authorityRequst/authorityRequst'
						})
					}
				});
			}
			
		}
	}
</script>

<style>
	.index-list1>view:first-child {
		color: #999999;
	}

	.index-list1>view:first-child image {
		width: 85upx;
		height: 85upx;
		border-radius: 100%;
		margin-right: 10upx;
	}

	.index-list1>view:last-child {
		background: #F4F4F4;
		border-radius: 5upx;
		padding: 0 10upx;
	}
	.load-more {
		text-align: center;
		color: #AAAAAA;
		padding: 15upx;
	}
	.refresh-button {
		position: fixed;
		bottom: 200upx;
		right: 30upx;
		width: 90upx;
		height: 90upx;
		font-size: 60upx;
		color: #007AFF;
	}
	.uni-media-list-logo {
		width: 60upx;
		height: 60upx;
	}
	
	.uni-media-list-body {
		height: auto;
		justify-content: space-around;
	}
	
	.uni-media-list-text-top {
		height: 74upx;
		font-size: 28upx;
		overflow: hidden;
		padding-top: 20upx;
		
	}
	.uni-media-list-text-top2 {
		height: 74upx;
		width: 100%;
		font-size: 28upx;
		overflow: hidden;
		padding-top: 20upx;
		
	}
	
	.uni-media-list-text-bottom {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}
	
</style>
